﻿/**
*
*
*	GestureDemo
*	
*	@author	Foxy
*	@version	1.0
*
*/

package {
	
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;


	
	import com.foxaweb.ui.gesture.*;
		
	public class GestureDemo extends MovieClip {

		// ------------------------------------------------
		//
		// ---o properties
		//
		// ------------------------------------------------
		
		private var mg:MouseGesture;
		
		public var out_tf:TextField;
		public var fiab_mc:MovieClip;
		public var draw_mc:MovieClip;
		
		private var timer:Timer;
		private var fadeTween:Tween;
		private var fiabTween:Tween;

		// ------------------------------------------------
		//
		// ---o constructor
		//
		// ------------------------------------------------
		
		public function GestureDemo(){
			
			stage.scaleMode="noScale";

			mg=new MouseGesture(stage);		
			mg.addGesture("A","71");
			mg.addGesture("B","260123401234");
			mg.addGesture("C","43210");
			mg.addGesture("D","26701234",dMatch);
			mg.addGesture("E","4321043210");
			mg.addGesture("F","42");
			mg.addGesture("G","432107650",gMatch);
			mg.addGesture("H","267012");
			mg.addGesture("I","2");
			mg.addGesture("J","234");
			mg.addGesture("K","3456701");
			mg.addGesture("L","20");
			mg.addGesture("M","6172");
			mg.addGesture("N","616");
			mg.addGesture("O","432107654",oMatch);
			mg.addGesture("P","26701234",pMatch);
			mg.addGesture("Q","4321076540",qMatch);
			mg.addGesture("R","267012341");
			mg.addGesture("S","432101234",sMatch);
			mg.addGesture("T","02");
			mg.addGesture("U","21076");
			mg.addGesture("V","17");
			mg.addGesture("W","2716");
			mg.addGesture("X","1076543");
			mg.addGesture("Y","21076234567");
			mg.addGesture("Z","030");
			
			mg.addGesture("0","4321076542");
			mg.addGesture("1","2");
			mg.addGesture("2","701230");
			mg.addGesture("3","0123401234");
			mg.addGesture("4","302");
			mg.addGesture("5","4201234",fiveMatch);
			mg.addGesture("6","43210765",sixMatch);
			mg.addGesture("7","03");
			mg.addGesture("8","4321234567654");
			mg.addGesture("9","43210762");
			
			mg.addGesture("BACKSPACE","4");
			mg.addGesture("SPACE","0");
			mg.addGesture("DOT",".");
			
			timer=new Timer(500,1);
			timer.addEventListener(TimerEvent.TIMER,fadeDrawing);
			
			mg.addEventListener(GestureEvent.GESTURE_MATCH,matchHandler);
			mg.addEventListener(GestureEvent.NO_MATCH,noMatchHandler);
			mg.addEventListener(GestureEvent.START_CAPTURE,startHandler);
			mg.addEventListener(GestureEvent.STOP_CAPTURE,stopHandler);
			
		}	

		// ------------------------------------------------
		//
		// ---o private methods
		//
		// ------------------------------------------------
		
		/**
		* match gesture handler
		*/
		protected function matchHandler(e:GestureEvent):void{
		
			switch (e.datas){
				case "BACKSPACE":
					out_tf.text=out_tf.text.substr(0,out_tf.text.length-1);
				break;
				case "SPACE":
					out_tf.text+=" ";
				break;
				case "DOT":
					out_tf.text+=".";
				break;
				default:
					out_tf.text+=e.datas;
				break;
			}
			
			fiab_mc.barre_mc.x=-6*e.fiability;
			timer.start();
		}
		
		/**
		* no match handler 
		*/	
		protected function noMatchHandler(e:GestureEvent):void{
			draw_mc.zone_mc.graphics.clear();
		}
		
		/**
		* start capturing phase handler
		*/
		protected function startHandler(e:GestureEvent):void{
			
			fiab_mc.alpha=1;
			fiab_mc.barre_mc.x=-119;
			
			timer.stop();
			if (fadeTween)fadeTween.stop();
			if (fiabTween)fiabTween.stop();
			
			draw_mc.zone_mc.graphics.clear();
			draw_mc.alpha=1;
			draw_mc.zone_mc.graphics.lineStyle(4,0x444444);
			draw_mc.zone_mc.graphics.moveTo(mouseX,mouseY);
			addEventListener(Event.ENTER_FRAME,capturingHandler);
		}
		
		/**
		* fade the drawing zone
		*/
		protected function fadeDrawing(e:TimerEvent):void{
			 fadeTween = new Tween(draw_mc,"alpha",Regular.easeIn,draw_mc.alpha,0,20);
			 fiabTween = new Tween(fiab_mc,"alpha",Regular.easeIn,fiab_mc.alpha,0,20);
		}
				
		/**
		* stop capturing phase handler
		*/
		protected function stopHandler(e:GestureEvent):void{
			removeEventListener(Event.ENTER_FRAME,capturingHandler);
		}
		
		/**
		* capturing handler
		*/
		protected function capturingHandler(e:Event):void{
			draw_mc.zone_mc.graphics.lineTo(mouseX,mouseY);
		}
		
		/**
		* G - Special case
		*/
		protected function gMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py>.3 ? infos.cost : 10000;
		}
		
		/**
		* Q - Special case
		*/
		protected function qMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py<.3 ? infos.cost : 10000;
		}
		
		/**
		* D - Special case
		*/
		protected function dMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py>.8 ? infos.cost : 10000;
		}
		
		/**
		* P - Special case
		*/
		protected function pMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py<.7 ? infos.cost : 10000;
		}
		
		/**
		* O - Special case
		*/
		protected function oMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py<.3 ? infos.cost : 10000;
		}

		/**
		* 6 - Special case
		*/
		protected function sixMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py>.3 ? infos.cost : 10000;
		}
		
		/**
		* 5 - Special case
		*/
		protected function fiveMatch(infos:Object):uint{
			var pos:int=infos.moves.join("").indexOf("111");
			return pos==-1 ? infos.cost : 10000;
		}
		
		/**
		* S - Special case
		*/
		protected function sMatch(infos:Object):uint{
			var pos:int=infos.moves.join("").indexOf("111");
			return pos>-1 ? infos.cost : 10000;
		}
		
		
		
	}
	
}

